package com.example.closethreadgracefully.controller;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

/**
 * @ClassName MyRunnable
 * @Description
 * @Author huyingliang
 * @Date 2024-04-24 16:03
 */
public class MyRunnable2 extends Thread {


    @Override
    public void run() {/*该线程能验证两个问题 第一个 如果使用 interrupted 方法去结束线程
    1.能不能真的结束线程   ---  经检验 可以
    2.在线程休眠的时候能不能也结束线程 ---  经检验 可以*/
        while (!Thread.currentThread().isInterrupted()) {

            for (int i = 0; i < 1000; i++) {
                if (!Thread.currentThread().isInterrupted()) {
                    System.out.println("i = " + i);
                    /*Request www.baidu.com*/
                    CloseableHttpClient httpClient = HttpClients.createDefault();

                    try {
                        httpClient.execute(new HttpGet("http://www.baidu.com"));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            /*执行完成业务后，休眠一会 然后继续下一轮执行 例如，休息30分钟*/
            try {
                System.out.println("休息30分钟");
                Thread.sleep(1000L * 60 * 30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
